<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限控制</title>
    <link href="common/css/bootstrap.min.css" rel="stylesheet">
    <script type="text/javascript" src="common/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="common/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="common/js/new_common.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">后台演示 </a>
        </div>
        <div>
            <ul class="nav navbar-nav">
                <li><a href="./webSocket.html">webSocket测试</a></li>
                <li><a href="./editRole.html">角色</a></li>
                <li><a href="./editAdminRole.html">管理员-角色关系</a></li>
                <li><a href="./editAccess.html">权限</a></li>
                <li><a href="./editRoleAccess.html">角色-权限关系</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                        管理员 <b class="caret"></b>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="./admininfo.html">用户信息</a></li>
                        <li><a href="./logout.html">退出登录</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>



<!--

`title` varchar(50) NOT NULL DEFAULT '' COMMENT '权限名称',
`urls` varchar(1000) NOT NULL DEFAULT '' COMMENT 'json 数组',

-->
<table class="table table-striped">
    <caption>权限列表</caption>
    <thead>
    <tr>
        <th>权限名</th>
        <th>对应URL</th>
        <th>创建时间</th>
        <th>最后一次更新时间</th>
        <th>软删除时间</th>
        <th>更改该角色</th>
        <th>删除该橘色</th>
    </tr>
    </thead>
    <tbody id="list">

    </tbody>
    <button id="showList" class="btn btn-success">展示角色列表</button>
</table>

<form class="form-horizontal" role="form">
    <div class="form-group has-success">
        <label class="col-sm-2 control-label" for="title">权限名称</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="title" name="title">
        </div>
    </div>

    <div class="form-group has-success">
        <label class="col-sm-2 control-label" for="urls">权限地址</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="urls" name="urls">
        </div>
    </div>


</form>
<button id="add" class="btn btn-success">添加权限</button>

<!-- 通用框（Modal） -->
<div class="modal fade" id="commonModal" tabindex="-1" role="dialog"
     aria-labelledby="commonModalLabel" aria-hidden="true" style=" z-index: 100000;">
    <div class="modal-dialog" style="margin-top: 12rem;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="commonModalLabel" style="text-align: center;">温馨提示</h4>
            </div>
            <div class="modal-body" id="commonModalBody" style="text-align: center;">
            </div>
            <div class="modal-footer" style="text-align: center;" id="commonModalSubmitCkeck"></div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->

<script>

    var type;

    $(document).ready(function () {
        roleList();
    });

    $('#add').bind('click', function () {
        type = 'add';
        var title = $("input[name='title']").val();
        var urls = $("input[name='urls']").val();
        var jsonStr = {'title': title, 'urls': urls};
        var url = "../api/rbac/addAccess";
        submit(url,jsonStr);
    });

    /**
     * 获取权限列表
     */
    function roleList() {
        type = 'list';
        var jsonStr = {'random':Math.random()};
        var url = "../api/rbac/listAccess";
        submit(url,jsonStr);
    }

    // 删除一条权限
    function deleteOne(id) {
        type = 'delete';
        var jsonStr = {'id':id};
        var url = "../api/rbac/deleteAccess";
        submit(url,jsonStr);
    }

    function updateOne(id,title,urls) {
        $('#commonModal').modal({keyboard: true});
        $("#commonModalLabel").html('更改角色');
        $("#commonModalBody").empty();
        $("#commonModalSubmitCkeck").empty();
        $("#commonModalBody").append(
           '<label class="col-sm-12 control-label" style="margin: 0 auto;">标题：</label>' +
            '<input name="uTitle" class="form-control" value="'+title+'"><br/>'+
            '<label class="col-sm-12 control-label" style="margin: 0 auto;">对应URLS：</label>' +
            '<input name="uUrls" class="form-control" value="'+urls+'"><br/>'
        );
        var htmlSubBtn = '<button type="button" class="btn btn-default" id="uSubmit" data-dismiss="modal">确定</button>';
        $("#commonModalSubmitCkeck").append(htmlSubBtn);

        $('#uSubmit').bind('click',function () {
            type = 'update';
            var title = $("input[name='uTitle']").val();
            var urls = $("input[name='uUrls']").val();
            var jsonStr = {'id':id,'title':title,'urls':urls};
            var url = "../api/rbac/updateAccess";
            submit(url,jsonStr);
        });

    }

    function submit(url,jsonStr) {
        $.ajax({
            url: url,//   请求地址
            type: 'post',//   请求方法
            timeout: 6000,//   超时时间
            async: true,//   true(默认):异步请求,false:同步请求(将锁住浏览器,请求完成才能操作浏览器其它选项)
            beforeSend: function (XMLHttpRequest) {
            },//    请求前执行的操作
            cache: false,//   true,使用缓存;false(默认),不使用缓存
            complete: function (XMLHttpRequest, textStatus) {
            },//    请求后执行的操作,不管请求是否成功都执行
            contentType: 'application/x-www-form-urlencoded',//  发送至服务器时的编码类型,默认适合大多数应用场景
            data: jsonStr,//  发送至服务器的数据参数,以 key/value 格式发送
            dataType: 'json',//  默认服务器返回的格式,有: xml\html\script\json\jsonp
            error: function (XMLHttpRequest, textStutus, errorThrown) {
            },//    请求失败将调用此方法
            global: true,//  是否出发全局ajax事件 , true,是; false(默认),否;
            ifModified: false,//  仅在服务器数据改变时捕获新数据,默认false
            processData: true,//     默认true,默认情况下,发送的数据将被转换为对象
            success: function (result, textStatus) { //     请求成功返回回调值,并执行的方法
                var sStatus = result.result === 'success';
                switch (type) {
                    case 'list':
                        var list = result.data;
                        var htmlArr = [];
                        for (var i = 0; i < list.length; i++) {
                            var html = ' <tr>\n' +
                                '        <td>' + list[i].title + '</td>\n' +
                                '        <td>' + list[i].urls + '</td>\n' +
                                '        <td>' + list[i].create_time + '</td>\n' +
                                '        <td>' + list[i].update_time + '</td>\n' +
                                '        <td>' + list[i].delete_time + '</td>\n' +
                                '        <td style="color: green;" ' +
                                'onclick="updateOne(\'' + list[i].id + '\',\''+list[i].title+'\',\''+list[i].urls+'\')"' +
                                '>更改</td>\n' +
                                '        <td style="color: red;" onclick="deleteOne(\'' + list[i].id + '\')">删除</td>\n' +
                                '    </tr>';
                            htmlArr.push(html);
                        }
                        $('#list').empty();
                        $('#list').append(htmlArr.join(''));
                        break;
                    case 'add':
                    case 'update':
                    case 'delete':
                        if(sStatus)
                            roleList();
                        break;

                }

            }
        });
    }

</script>

</body>
</html>

